<?php

namespace app\admin\model\news;

use think\Model;


class News extends Model
{

    

    

    // 表名
    protected $name = 'news';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'news_published_time_text',
        'news_is_show_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['news_sort' => $row[$pk]]);
        });
    }

    
    public function getNewsIsShowList()
    {
        return ['0' => __('News_is_show 0'), '1' => __('News_is_show 1')];
    }


    public function getNewsPublishedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['news_published_time']) ? $data['news_published_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getNewsIsShowTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['news_is_show']) ? $data['news_is_show'] : '');
        $list = $this->getNewsIsShowList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setNewsPublishedTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function newstype()
    {
        return $this->belongsTo('app\admin\model\news\NewsType', 'news_type', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
